import { nanoid10 } from "../../../../tools/Tools";
//获取交集数据
export function toIntersect(arr1, arr2) {
    let result = [];
    if (!arr1.length || !arr1.length) {
        return [];
    }
    for (let i = 0; i < arr1.length; i++) {
        if (arr2.includes(arr1[i])) {
            result.push(arr1[i]);
        }
    }
    return result;
}
//对象to数组
export function objToArray(obj, rowId) {
    let noSelectedData = [];
    let selectedData = [];
    for (let key in obj) {
        if (key !== rowId) {
            console.log("进入条件", obj[key]);
            noSelectedData = noSelectedData.concat(obj[key]);
        }
        else {
            selectedData = selectedData.concat(obj[key]);
        }
    }
    return {
        noSelectedData,
        selectedData,
    };
}
//cpatable data to mergerow data
export function cpasToRowMerge(data) {
    let queryTemplateList = [];
    let queryRowsList = [];
    let queryTemplateCellData = {};
    if (data && data.length) {
        for (let i = 0; i < data.length; i++) {
            const item = data[i];
            const id = item.id;
            queryTemplateList.push({
                id: id,
                value: item.value,
            });
            queryTemplateCellData[id] = item.cellData;
            const checkedData = item.cellData;
            if (checkedData && checkedData.length) {
                for (let j = 0; j < checkedData.length; j++) {
                    queryRowsList.push(checkedData[j]);
                }
            }
        }
    }
    return {
        queryTemplateList,
        queryRowsList,
        queryTemplateCellData,
    };
}
//格式化下拉框子级数据
export function formatChildTree(child, parentIndex, type) {
    if (!child || !child.length) {
        return [];
    }
    let arr = [];
    for (let j = 0; j < child.length; j++) {
        const current = child[j];
        let children = [];
        if (current.children && current.children.length) {
            const index = j + parentIndex;
            children = formatChildTree(current.children, index, type);
        }
        const isRuntime = (!children.length && current?.config?.isRuntime) || children.length;
        if (current?.config?.type === type && !isRuntime) {
            arr.push({
                label: current.label,
                value: `${current.label}-${parentIndex}-${j}`,
                columnNum: j + parentIndex,
                children,
            });
        }
        if (type === "all" && !isRuntime) {
            arr.push({
                label: current.label,
                value: `${current.label}-${parentIndex}-${j}`,
                columnNum: j + parentIndex,
                children,
            });
        }
    }
    return arr;
}
//判断header数据是否有匹配的类型
export function isChildDataType(child, type) {
    if (!child) {
        return false;
    }
    const res = child.filter((item) => (item?.type || item?.config?.type) === type);
    return res && res.length ? true : false;
}
//模拟新增数据。重新计算row坐标
function toAdd(rows, spliceNum) {
    let after = rows.slice(spliceNum);
    after = after.map((x) => {
        if (x.newRow) {
            x["newRow"] = x.newRow + 1;
        }
        else if (x.row && !x.newRow) {
            x["newRow"] = x.row + 1;
        }
        return x;
    });
    after.unshift({});
    let before = rows.slice(0, spliceNum);
    before = before.map((x) => {
        if (x.newRow) {
            x["newRow"] = x.newRow;
        }
        else if (x.row && !x.newRow) {
            x["newRow"] = x.row;
        }
        return x;
    });
    return before.concat(after);
}
//模拟删除数据，重新计算row坐标
function toDel(rows, spliceNum) {
    let after = rows.slice(spliceNum);
    after.shift();
    after = after.map((x) => {
        if (x.newRow) {
            x["newRow"] = x.newRow - 1;
        }
        else if (x.row && !x.newRow) {
            x["newRow"] = x.row - 1;
        }
        return x;
    });
    let before = rows.slice(0, spliceNum);
    before = before.map((x) => {
        if (x.newRow) {
            x["newRow"] = x.newRow;
        }
        else if (x.row && !x.newRow) {
            x["newRow"] = x.row;
        }
        return x;
    });
    return before.concat(after);
}
/**行归并数据merge*/
function sumOfColumns(array, sourceData, rowMetaData) {
    const newData = [...sourceData];
    let newRowMeta = [...rowMetaData];
    let result = [];
    console.log(array, "array before");
    for (let i = 0; i < array.length; i++) {
        console.log(array[i], "array 当前要合并的数据");
        const mergeData = array[i].mergeData;
        if (mergeData.length) {
            let sums = new Array(array[i].mergeData[0].length).fill(0); //创建一个数组用来存储每列的和
            const columnNum = array[i].columnNum;
            const spliceNum = array[i].spliceNum;
            const delRowIds = array[i].delRowIds;
            for (let j = 0; j < mergeData.length; j++) {
                for (let k = 0; k < mergeData[j].length; k++) {
                    if (typeof mergeData[j][k] === "string") {
                        if (k == 0) {
                            sums[k] = sums[k] === 0 ? "" : sums[k];
                            sums[k] += `${mergeData[j][k]}-`; //将字符串进行拼接操作
                        }
                        else if (k === mergeData[j].length - 1) {
                            sums[k] = JSON.stringify({ rId: nanoid10() });
                        }
                        else {
                            sums[k] = mergeData[0][k]; //将字符串进行拼接操作
                        }
                    }
                    else {
                        sums[k] += mergeData[j][k]; //将数字进行求和操作
                    }
                }
            }
            //更新行模版数据
            if (columnNum && array[i].value) {
                sums[columnNum] = array[i].value;
            }
            newData.splice(spliceNum, 0, sums);
            newRowMeta = toAdd(newRowMeta, spliceNum);
            result.push(sums);
            //匹配到对应rid，进行数据删除
            for (let j = 0; j < delRowIds.length; j++) {
                const rId = delRowIds[j];
                for (let k = 0; k < newData.length; k++) {
                    const rowData = newData[k];
                    const rowStr = rowData[rowData.length - 1];
                    const rowCfg = rowStr ? JSON.parse(rowStr) : {};
                    if (rowCfg.rId === rId) {
                        newData.splice(k, 1);
                        newRowMeta = toDel(newRowMeta, k);
                    }
                }
            }
        }
    }
    return {
        newData,
        newRowMeta,
    };
}
/**根据CellMeta获取RowMeta */
function getRowMeta(array) {
    let temp = {};
    let rowMeta = [];
    for (let i = 0; i < array.length; i++) {
        const row = array[i].row;
        temp[row] = row;
    }
    for (let key in temp) {
        const row = temp[key];
        rowMeta.push({
            row,
        });
    }
    return rowMeta;
}
function diffCellsMeta(oldCellMeta, newRowMeta) {
    let newCellMeta = [...oldCellMeta];
    for (let i = 0; i < newCellMeta.length; i++) {
        const row = newCellMeta[i].row;
        for (let j = 0; j < newRowMeta.length; j++) {
            const oldRow = newRowMeta[j].row;
            if (row === oldRow && newRowMeta[j].newRow) {
                newCellMeta[i].row = newRowMeta[j].newRow;
            }
        }
    }
    return newCellMeta;
}
function formatCpasData(rowsData, sourceData) {
    let rowSum = [];
    for (let i = 0; i < rowsData.length; i++) {
        const mergeRowIds = rowsData[i].mergeRowIds;
        let mergeData = [];
        let spliceNum = 0;
        for (let j = 0; j < mergeRowIds.length; j++) {
            const mergeRid = mergeRowIds[j];
            for (let k = 0; k < sourceData.length; k++) {
                const rowData = sourceData[k];
                const rowStr = rowData[rowData.length - 1];
                const rowCfg = rowStr ? JSON.parse(rowStr) : {};
                if (j === 0 && rowCfg.rId === mergeRid) {
                    spliceNum = k;
                }
                if (rowCfg.rId === mergeRid) {
                    mergeData.push(sourceData[k]);
                }
            }
        }
        rowSum.push({
            value: rowsData[i].value,
            mergeData,
            columnNum: rowsData[i].columnNum,
            spliceNum: spliceNum + i,
            delRowIds: mergeRowIds, //删除的id
        });
    }
    return rowSum;
}
//cpastable源数据(经过用户设置条件)合并为新cpastable可展示数据
export function cpasSourceDataToMergeData(dsSourceData, newTableCfg) {
    console.log(dsSourceData, "dsSourceData111");
    const sourceData = dsSourceData.data ? dsSourceData.data : [];
    const cellsMeta = dsSourceData.cellsMeta ? dsSourceData.cellsMeta : [];
    const { rowMergeCfg = {} } = newTableCfg || {};
    const { rowsData } = rowMergeCfg;
    if (!sourceData && !sourceData.length) {
        return;
    }
    if (!rowsData || !rowsData.length) {
        return;
    }
    const newSouceData = sourceData.slice(1);
    const firstRowData = sourceData.slice(0, 1);
    console.log(rowsData, "rowsData1111");
    const rowSum = formatCpasData(rowsData, newSouceData);
    console.log(rowSum, "rowSum111");
    const rowMetaData = getRowMeta(cellsMeta);
    const { newData, newRowMeta } = sumOfColumns(rowSum, newSouceData, rowMetaData);
    const newCellsMeta = diffCellsMeta(cellsMeta, newRowMeta);
    return {
        ...dsSourceData,
        cellsMeta: newCellsMeta,
        data: [...firstRowData, ...newData],
    };
}
